 /*
    *  Copyright 2004 Thevert Liu  <thevert.liu@gmail.com>
    *
    *  Licensed under the Apache License, Version 2.0 (the "License");
    *  you may not use this file except in compliance with the License.
    *  You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
   *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
#ifndef X2_STRING_H
#define X2_STRING_H

#include <string>
#include <vector>


namespace x2 {


class UTF8Helper
{
public:
static    std::wstring& UTF8ToUTF16(const std::string& str, std::wstring& dest) ;
static    std::string & UTF16ToUTF8(const std::wstring& str, std::string& dest) ;

private:

static    wchar_t read_char (const char* str, size_t& seek) ;
static    void    write_char(wchar_t t, std::string& to) ;

    virtual void dummy() = 0 ;

} ;


class Tokens : public std::vector<std::string>
{
public:
    
    size_t find(const std::string& s) ;
    
} ;


class  string : public std::string
{
public:
    static string null ;

    typedef std::string::size_type size_type ;
    
    string() : std::string() {}
    string(const char* s) : std::string(s) {}

    string(const std::string& str) : std::string(str) { }
    
    bool from(bool v) ;
    bool from(int v) ;
    bool from(double v) ;
    bool from(float v) ;
    bool from_hex(unsigned int x) ;
    
    bool to(bool v) const  ;
    bool to(int v) const ;
    bool to(double v) const ;
    bool to(float v) const ;
    bool to_hex(unsigned int v) const ;
    int  to_hex() const {int t; to_hex(t); return t ;}

    int  to_int() const {int t;to(t);return t;}
    
static    bool is_digit(char c)  ;
static    bool is_hex  (char c)  ;
static    bool is_space(char c)  ;
    
    bool starts_with(const std::string& s) const;
    
    inline operator bool() const {return !empty() ;}
    
    string& to_upper() ;
    string& to_lower() ;
    
//    string& put(char c) ;
//    string& put(const char* s, size_t l) ;
//    string& put(const std::string& s) ;
    
    bool read(std::istream& is) ;
    
    bool iequals(const std::string& s) const ;
    
    bool has(char c) const ;
    bool has(const std::string& s) const ;
    bool hasi(const std::string& s) const ;

    string& to_utf8(const std::wstring& ws) ;    
    
    std::wstring& to_utf16(std::wstring& ws) const ;
    std::wstring to_utf16() const {std::wstring t; return to_utf16(t);}

    bool tokens(Tokens& tokens, const std::string& whitechars = "\t\r\n ") const ;
    Tokens tokens(const std::string& whitechars = "\t\r\n ") const ;

    bool split(const std::string& s, std::string& left, std::string& right, const std::string& whitechars = "\t\r\n ") const ;
    
} ;

}

#endif //X2_STRING_H

